package com.huasheng.infohub.dao.impl;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;

import com.huasheng.infohub.dao.EmailDao;

public class EmailDaoMysql implements EmailDao {
	
	private Connection conn = null;
	private QueryRunner run;
	
	public boolean init() {
		boolean bsuccess = false;
		
		Context context;
		try {
			context = new InitialContext();
			DataSource ds = (DataSource) context.lookup("java:/comp/env/jdbc/info-test");
			conn = ds.getConnection();
			run = new QueryRunner();
			
			bsuccess = true;
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return bsuccess;
	}
	
	@Override
	public boolean insert(String email, long id, long orgId) throws SQLException{					
		run.update(conn, "INSERT INTO t_email(email, org_id, user_id) VALUES (?, ?, ?)", 
					email, orgId, id);		
		
		return true;
	}
	
	public void end() {
		try {
			DbUtils.close(conn);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
